<template>
    <div class="tabs-item" @click="onClick" :class="classes" :data-name="name">
        <slot></slot>
    </div>
</template>

<script>
    export default {
        name: "TABSITEM",
        props: {
            disabled: {
                type: Boolean,
                default: false
            },
            name: {
                type: [String, Number],
                required: true
            }
        },
        inject: ['eventBus'],
        computed: {
            classes(){
                return {
                    active: this.active,
                    disabled: this.disabled
                }
            }
        },
        data () {
            return {
                active: false
            }
        },
        created() {
            if(this.eventBus){
                this.eventBus.$on('update:selected', (name) => {
                    this.active = name === this.name;
                })
            }
        },
        methods: {
            onClick(){
                if(this.disabled){return}
                this.eventBus && this.eventBus.$emit('update:selected', this.name, this)
                this.$emit('click', this) //用于测试
            }
        }
    }
</script>

<style lang="scss" scoped>
    $black: black;
    $disabled-text-color: gray;
    .tabs-item{
        flex-shrink: 0;
        padding: 0 1em;
        cursor: pointer;
        height: 100%;
        display: flex;
        align-items: center;
        &.active{
            font-weight: bold;
            color: $black;
        }
        &.disabled{
            color: $disabled-text-color;
            cursor: not-allowed;
        }
    }
</style>